package com.boot.main.service.impl;

import com.boot.main.utils.IsNotEmptyUtil;
import com.boot.main.entity.CcomPermission;
import com.boot.main.mapper.CcomPermissionMapper;
import com.boot.main.response.ResponseData;
import com.boot.main.response.ResponseDataUtil;
import com.boot.main.service.IPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PermissionService implements IPermissionService {
    @Resource
    private CcomPermissionMapper permissionMapper;

    @Override
    public ResponseData getPermissions(Integer page, Integer limit, String name){
        List<CcomPermission> items = permissionMapper.getPermissions((page-1)*limit, limit, name);
        Integer count = permissionMapper.getTotalCount();
        Map map = new HashMap();
        map.put("datas", items);
        map.put("count", count);
        if(!items.isEmpty()){
            for (CcomPermission item:items){

            }
            return ResponseDataUtil.success( "查询成功", map);
        }
        return ResponseDataUtil.success("暂无权限信息！", map);
    }

    @Override
    public ResponseData savePermission(CcomPermission item){
        int item_num = permissionMapper.savePermission(item);
        long item_id = item.getId();
        if(!IsNotEmptyUtil.isEmpty(item_num)) {
            return ResponseDataUtil.success( "新增权限成功！", null);
        } else {
            return ResponseDataUtil.error("新增权限失败！", null);
        }
    }

    @Override
    public ResponseData updatePermission(CcomPermission item){
        int item_num = permissionMapper.updatePermission(item);
        if(!IsNotEmptyUtil.isEmpty(item_num)) {
            return ResponseDataUtil.success( "修改权限成功！", null);
        } else {
            return ResponseDataUtil.error( "修改权限失败！", null);
        }
    }

    @Override
    public ResponseData deletePermission(Long id){
        int item_num = permissionMapper.deletePermission(id);
        if(!IsNotEmptyUtil.isEmpty(item_num)) {
            return ResponseDataUtil.success("删除权限成功！", null);
        } else {
            return ResponseDataUtil.error( "删除权限失败！", null);
        }
    }
}
